<#
.SYNOPSIS
Windows PowerShell 代理服务器配置脚本

.DESCRIPTION
配置 Windows 系统级和开发工具代理设置
#>

param(
    [string]$ProxyServer = "192.168.31.7:17897",
    [string]$BackupProxy = "127.0.0.1:7890"
)

function Test-ProxyConnection {
    param([string]$Proxy)
    try {
        $testUrl = "http://www.google.com"
        $webClient = New-Object System.Net.WebClient
        $webClient.Proxy = New-Object System.Net.WebProxy($Proxy)
        $webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
        $result = $webClient.DownloadString($testUrl)
        return $true
    }
    catch {
        return $false
    }
}

function Set-SystemProxy {
    param([string]$Proxy)
    
    Write-Host "配置系统代理: $Proxy" -ForegroundColor Green
    
    # 设置系统代理
    netsh winhttp set proxy "$Proxy" "<local>"
    
    # 设置环境变量
    [System.Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://$Proxy", "User")
    [System.Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://$Proxy", "User")
    [System.Environment]::SetEnvironmentVariable("ALL_PROXY", "http://$Proxy", "User")
    
    Write-Host "系统代理配置完成" -ForegroundColor Green
}

function Set-GitProxy {
    param([string]$Proxy)
    
    Write-Host "配置 Git 代理..." -ForegroundColor Yellow
    
    git config --global http.proxy "http://$Proxy"
    git config --global https.proxy "http://$Proxy"
    
    # 配置 GitHub 特定代理
    git config --global http.https://github.com.proxy "http://$Proxy"
    
    Write-Host "Git 代理配置完成" -ForegroundColor Green
}

function Set-NpmProxy {
    param([string]$Proxy)
    
    Write-Host "配置 npm 代理..." -ForegroundColor Yellow
    
    npm config set proxy "http://$Proxy"
    npm config set https-proxy "http://$Proxy"
    npm config set registry "https://registry.npmmirror.com/"
    
    Write-Host "npm 代理配置完成" -ForegroundColor Green
}

function Set-PipProxy {
    param([string]$Proxy)
    
    Write-Host "配置 pip 代理..." -ForegroundColor Yellow
    
    # 创建 pip 配置文件
    $pipConfig = @"
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
trusted-host = pypi.tuna.tsinghua.edu.cn
proxy = http://$Proxy
"@
    
    $pipDir = "$env:USERPROFILE\pip"
    if (!(Test-Path $pipDir)) {
        New-Item -ItemType Directory -Path $pipDir -Force
    }
    
    $pipConfig | Out-File -FilePath "$pipDir\pip.ini" -Encoding UTF8
    
    Write-Host "pip 代理配置完成" -ForegroundColor Green
}

function Set-CargoProxy {
    param([string]$Proxy)
    
    Write-Host "配置 Cargo 代理..." -ForegroundColor Yellow
    
    # 创建 Cargo 配置文件
    $cargoConfig = @"
[registry]
default = "rustcc"

[source.rustcc]
registry = "https://code.rustcc.com.cn/crates.io-index"

[net]
git-fetch-with-cli = true

[http]
proxy = "http://$Proxy"
check-revoke = false

[https]
proxy = "http://$Proxy"
"@
    
    $cargoDir = "$env:USERPROFILE\.cargo"
    if (!(Test-Path $cargoDir)) {
        New-Item -ItemType Directory -Path $cargoDir -Force
    }
    
    $cargoConfig | Out-File -FilePath "$cargoDir\config" -Encoding UTF8
    
    Write-Host "Cargo 代理配置完成" -ForegroundColor Green
}

# 主程序
Write-Host "=== 代理服务器配置工具 ===" -ForegroundColor Cyan
Write-Host "主要代理: $ProxyServer" -ForegroundColor Yellow
Write-Host "备用代理: $BackupProxy" -ForegroundColor Yellow

# 测试代理连接
Write-Host "`n测试代理连接..." -ForegroundColor Cyan

if (Test-ProxyConnection -Proxy $ProxyServer) {
    Write-Host "主要代理可用" -ForegroundColor Green
    $activeProxy = $ProxyServer
}
elseif (Test-ProxyConnection -Proxy $BackupProxy) {
    Write-Host "主要代理不可用，使用备用代理" -ForegroundColor Yellow
    $activeProxy = $BackupProxy
}
else {
    Write-Host "所有代理均不可用，跳过配置" -ForegroundColor Red
    exit 1
}

# 配置各项代理
Set-SystemProxy -Proxy $activeProxy
Set-GitProxy -Proxy $activeProxy
Set-NpmProxy -Proxy $activeProxy
Set-PipProxy -Proxy $activeProxy
Set-CargoProxy -Proxy $activeProxy

Write-Host "`n=== 代理配置完成 ===" -ForegroundColor Cyan
Write-Host "当前使用代理: $activeProxy" -ForegroundColor Green
Write-Host "请重启终端或重新加载环境变量使配置生效" -ForegroundColor Yellow